package com.liy.framework.common.utils;

import java.security.*;
import javax.crypto.spec.*;
import java.security.spec.*;
import javax.crypto.*;
import sun.misc.*;

public class EncryptUtil
{
    private final byte[] DESIV;
    private AlgorithmParameterSpec iv;
    private Key key;
    private String charset;
    
    public EncryptUtil(final String deSkey, final String charset) throws Exception {
        this.DESIV = new byte[] { 18, 52, 86, 120, -112, -85, -51, -17 };
        this.iv = null;
        this.key = null;
        this.charset = "utf-8";
        final DESKeySpec keySpec = new DESKeySpec(deSkey.getBytes(this.charset));
        this.iv = new IvParameterSpec(this.DESIV);
        final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        this.key = keyFactory.generateSecret(keySpec);
    }
    
    public String encode(final String data) throws Exception {
        final Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        enCipher.init(1, this.key, this.iv);
        final byte[] pasByte = enCipher.doFinal(data.getBytes("utf-8"));
        final BASE64Encoder base64Encoder = new BASE64Encoder();
        return base64Encoder.encode(pasByte);
    }
    
    public String decode(final String data) throws Exception {
        final Cipher deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        deCipher.init(2, this.key, this.iv);
        final BASE64Decoder base64Decoder = new BASE64Decoder();
        final byte[] pasByte = deCipher.doFinal(base64Decoder.decodeBuffer(data));
        return new String(pasByte, "UTF-8");
    }
    
    public static void main(final String[] args) {
        try {
            final String test = "{'userId': 'TK_C61D6BEDBE07407FADFC3C31BA91FAFA','user234Id': 'TK_C61D6BEDBE07407FADFC3C334341BA91FAFA','user34Id': 'TK_C61D6BEDBE0723434407FADFC3C31BA91FAFA','userId': 'TK_C61D6BEDBE07407FADFC3C31BA91FAFA','user234Id': 'TK_C61D6BEDBE07407FADFC3C334341BA91FAFA','user34Id': 'TK_C61D6BEDBE0723434407FADFC3C31BA91FAFA','userId': 'TK_C61D6BEDBE07407FADFC3C31BA91FAFA','user234Id': 'TK_C61D6BEDBE07407FADFC3C334341BA91FAFA','user34Id': 'TK_C61D6BEDBE0723434407FADFC3C31BA91FAFA','userId': 'TK_C61D6BEDBE07407FADFC3C31BA91FAFA','user234Id': 'TK_C61D6BEDBE07407FADFC3C334341BA91FAFA','user34Id': 'TK_C61D6BEDBE0723434407FADFC3C31BA91FAFA'}";
            final String key = "9ba45bfd500642328ec03ad8ef1b6e75";
            final EncryptUtil des = new EncryptUtil(key, "utf-8");
            long s = System.currentTimeMillis();
            final String en = des.encode(test);
            long e = System.currentTimeMillis() - s;
            System.out.println("\u52a0\u5bc6\u65f6\u95f4\uff1a" + e);
            System.out.println("\u52a0\u5bc6\u540e\u7684\u5b57\u7b26\uff1a" + des.encode(test));
            s = System.currentTimeMillis();
            des.decode(en);
            e = System.currentTimeMillis() - s;
            System.out.println("\u89e3\u5bc6\u65f6\u95f4\uff1a" + e);
            System.out.println("\u89e3\u5bc6\u540e\u7684\u5b57\u7b26\uff1a" + des.decode(des.encode(test)));
        }
        catch (Exception e2) {
            e2.printStackTrace();
        }
    }
}
